#pragma once


#include <IXTab.h>
#include <IXRadioBox.h>
#include <IXLayout.h>

#include <vector>

#include "XFrameImpl.h"

struct stTabInfo
{
    XString     strName;
    IXRadioBox* pButton;
    IXFrame*    pFrame;
    IXTabContent* pContent;
};
typedef std::vector<stTabInfo> TabInfoVector;

template <class I>
class CXTabImpl : public CXFrameImpl<I>, public CXEventHandler<CXTabImpl<I>>
{
    typedef CXFrameImpl<I> superclass;

    BEGIN_DEF_SKIN_CLASS(CXTab, superclass, gs_strGuidXSkin)
        IMP_BASIC_INTF(IXTab,    0)
    END_DEF_SKIN_CLASS();

    IMPL_CONTROL_OBJ(CXTabImpl, superclass, _T("Tab"));

public:
    CXTabImpl();
    virtual ~CXTabImpl();

public:
    // IXTab
    virtual void    STDMETHODCALLTYPE   SetButtonMargin(int nMargin);
    virtual void    STDMETHODCALLTYPE   SetButtonSize(int nWidth, int nHeight);

    virtual void    STDMETHODCALLTYPE   SetTitleTextFont(IN IXFont* pFont);
    virtual void    STDMETHODCALLTYPE   SetTitleTextEnv(IN IXTextEnv* pTextEnv);
    virtual void    STDMETHODCALLTYPE   SetInternalMargin(IN int nLeft, IN int nTop, IN int nRight, IN int nBottom);

    virtual void    STDMETHODCALLTYPE   SetImageSize(IN int nImageWidth, IN int nImageHeight);

    virtual void    STDMETHODCALLTYPE   SetDirection(BOOL bHorz);

    virtual HRESULT STDMETHODCALLTYPE   InsertItem(int nIndex, LPCTSTR szTabName, IXTabContent* pContent);
    virtual HRESULT STDMETHODCALLTYPE   DeleteItem(LPCTSTR szTabName);

    virtual int     STDMETHODCALLTYPE   GetCurSel();
    virtual UINT    STDMETHODCALLTYPE   GetCount();

    virtual HRESULT STDMETHODCALLTYPE   ShowTab(IN LPCTSTR szTabName);

    virtual void    STDMETHODCALLTYPE   GetFrameRect(OUT RECT* prcFrame);
    virtual IXTabContent* STDMETHODCALLTYPE   GetContentByIndex(IN LPCTSTR szTabName);

protected:
    // CXControlImpl
    virtual void OnCreate();

protected:
    // XTabImpl
    size_t  FindItemByName(LPCTSTR szName);
    size_t  FindItemByItemId(LPCTSTR szId);

    HRESULT ShowTabByIndex(int nIndex);

    void    OnTabButtonClicked(IXControl* pSrcControl, LPARAM lParam);

    void    AdjustFramePos();

    void    InitTabButton(LPCTSTR szTabName, IXRadioBox* pButton, IXTabContent* pContent);

protected:
    TabInfoVector   m_vctTabInfo;

    BOOL            m_bHorz;
    SIZE            m_ButtonSize;
    int             m_nButtonMargin;
    SIZE            m_ImageSize;

    int             m_nCurSel;
    IXLayout*       m_pBtnLayout;

    IXFont*         m_pTitleFont;
    IXTextEnv*      m_pTitleTextEnv;

    RECT            m_rcInternalMargin;
};


class CXTab : public CXTabImpl<IXTab>
{
};
